package com.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * 作者 张超
 * 时间 2018/10/12
 * 项目 springboot-demo v1.0
 * 版本 v1.0
 */

@Configuration
public class RabbitMQConfig {
    private final static String change = "testExchange";
    private final static String ttlQueue = "ttl-queue";
    private final static String consumerQueue = "cunsumer-queue";

    //定义交换机
    @Bean
    public Exchange getExchange(){
        return new DirectExchange(change);
    }

    //定义过期队列
    @Bean
    public Queue getTTLQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-expires", 30000);//队列过期时间
        args.put("x-message-ttl", 12000);//队列上消息过期时间，应小于队列过期时间
        args.put("x-dead-letter-exchange", change);//过期消息转向路由
        args.put("x-dead-letter-routing-key", consumerQueue);//过期消息转向路由相匹配
        return new Queue(ttlQueue, true, false, false, args);

    }

    @Bean
    public Queue getCunsumer(){
        return new Queue(consumerQueue);
    }

    @Bean
    public BindingBuilder.GenericArgumentsConfigurer bindConsumer(){
        return BindingBuilder.bind(getCunsumer()).to(getExchange()).with("");
    }

    @Bean
    public BindingBuilder.GenericArgumentsConfigurer bindTTL() {
        return BindingBuilder.bind(getTTLQueue()).to(getExchange()).with(ttlQueue);
    }
}
